//
//  GYPropertyViewController.m
//  GoodLuck
//
//  Created by apple on 2018/3/5.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYPropertyViewController.h"
#import "GYAllPropertyCell.h"
#import "GYIntegralViewController.h"
#import "GYNetWorkTools.h"
#import "GYSearchViewController.h"
#import "GYBasePopoverViewController.h"
#import "PopoverAnimation.h"
#import "GYSystemMessageViewController.h"

@interface GYPropertyViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

/** 数据源 */
@property (nonatomic, strong) NSMutableArray *propertys;

/** 动画代理 */
@property (nonatomic, strong) PopoverAnimation *animationDelegate;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewBottomMarginCons;

@end

@implementation GYPropertyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableViewBottomMarginCons.constant = iPhoneX ? 44 : 0;
    //初始化
    [self setupUI];
}

- (void)setupUI {
    self.navigationItem.title = @"我的财产";
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"more_b" tagret:self action:@selector(rightButClick) button:nil];
    
    //设置表格的一些属性
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //加载数据
    [self loadUserProperty];
}

- (void)rightButClick {
    GYBasePopoverViewController *basePopoverVC = [[GYBasePopoverViewController alloc] initWithNibName:@"GYBasePopoverViewController" bundle:nil];
    __weak typeof(self) weakSelf = self;
    basePopoverVC.popoverViewSelectIndexCallBack = ^(NSInteger index) {
        switch (index) {
            case 0:
            {
                weakSelf.tabBarController.selectedIndex = 0;
                [weakSelf.tabBarController.tabBar setHidden:NO];
            }
                break;
                
            case 1:
            {
                GYSearchViewController *searchVC = [[GYSearchViewController alloc] init];
                [self.navigationController pushViewController:searchVC animated:YES];
            }
                break;
                
            case 2:
            {
                GYSystemMessageViewController *systemMessageVC = [[GYSystemMessageViewController alloc] initWithNibName:@"GYSystemMessageViewController" bundle:nil];
                [weakSelf.navigationController pushViewController:systemMessageVC animated:YES];

            }
                break;
                
            default:
                break;
        }
    };
    basePopoverVC.titles = @[@"首页",@"搜索",@"消息"];
    basePopoverVC.images = @[@"home_w",@"search_w",@"message_w"];
    
    self.animationDelegate = [[PopoverAnimation alloc] init];
    self.animationDelegate.popoverFrame = CGRectMake(ScreenWidth-widthScale(130, iphone6_W)-5, 50, widthScale(130, iphone6_W), basePopoverVC.titles.count*50);
    basePopoverVC.transitioningDelegate = (id)weakSelf.animationDelegate;
    
    basePopoverVC.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:basePopoverVC animated:YES completion:nil];

}


- (void)loadUserProperty {
    [EasyLodingView showLodingText:@"正在加载..." config:^EasyLodingConfig *{
        EasyLodingConfig *config = [EasyLodingConfig shared];
        config.showOnWindow = YES;
        
        return config;
    }];
    [GYNetWorkTools requestUserProperty:^(NSDictionary *obj) {
        int code = [[obj objectForKey:@"code"] intValue];
        NSDictionary *datas = [obj objectForKey:@"datas"];
        if (code == 200) {
            [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
            NSString *point = [NSString stringWithFormat:@"%@",[datas objectForKey:@"point"]];
            [self.propertys addObject:point];
            [self.tableView reloadData];
        }else if (code == 400) {
            [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
            [EasyTextView showText:@"加载出错,请稍后加载!"];
            NSLog(@"网络加载出错...");
        }
    } error:^(NSError *error) {
        [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
        [EasyTextView showText:@"加载出错,请稍后加载!"];
        NSLog(@"laodUserPropertyFailed=====%@",error);
    }];
}
#pragma mark -- UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.propertys.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYAllPropertyCell *cell = [GYAllPropertyCell cellWithTableView:tableView];
    cell.mccLable.text = self.propertys[indexPath.row];
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    GYIntegralViewController *integraVC = [[GYIntegralViewController alloc] initWithNibName:@"GYIntegralViewController" bundle:nil];
    [self.navigationController pushViewController:integraVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYAllPropertyCell *cell = [GYAllPropertyCell cellWithTableView:tableView];
    return [cell cellHeight];
}

#pragma mark -- 懒加载

- (NSMutableArray *)propertys {
    if (!_propertys) {
        _propertys = [NSMutableArray array];
    }
    
    return _propertys;
}
@end
